//
//  VSaleDateView.m
//  STL_ Animation
//
//  Created by dllo on 16/4/5.
//  Copyright © 2016年 STL. All rights reserved.
//

#import "VSaleDateView.h"
#import "STL.h"

@interface VSaleDateView ()

@property (nonatomic, strong) STLButton *prevButton;
@property (nonatomic, strong) STLButton *nextButton;
@property (nonatomic, strong) STLButton *dateButton;

@end

@implementation VSaleDateView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatButtons];
    }
    return self;
}

- (void)creatButtons {
    self.prevButton = [STLButton buttonWithType:UIButtonTypeCustom];
    self.prevButton.frame = CGRectMake(5, 5, BoundsHeight - 10, BoundsHeight - 10);
    self.prevButton.tag = 0;
    [self addSubview:self.prevButton];
    
    self.nextButton = [STLButton buttonWithType:UIButtonTypeCustom];
    self.nextButton.frame = CGRectMake(BoundsWidth - BoundsHeight + 5, 5, BoundsHeight - 10, BoundsHeight - 10);
    self.nextButton.tag = 1;
    [self addSubview:self.nextButton];
    
    self.dateButton = [STLButton buttonWithType:UIButtonTypeCustom];
    self.dateButton.frame = CGRectMake(BoundsHeight, 5, BoundsWidth - 2 * BoundsHeight, BoundsHeight - 10);
    [self addSubview:self.dateButton];
}

- (void)setPastListModel:(VSalePastListModel *)pastListModel {
    _pastListModel = pastListModel;
    if (_pastListModel.prevDateCode.intValue == 0) {
        [self.prevButton setBackgroundImage:[UIImage imageNamed:@"prev1"] forState:UIControlStateNormal];
    } else {
        [self.prevButton setBackgroundImage:[UIImage imageNamed:@"prev"] forState:UIControlStateNormal];
        [self.prevButton addTarget:self action:@selector(request:) forControlEvents:UIControlEventTouchUpInside];
    }
    if (_pastListModel.nextDateCode.intValue == 0) {
        [self.nextButton setBackgroundImage:[UIImage imageNamed:@"next1"] forState:UIControlStateNormal];
    } else {
        [self.nextButton setBackgroundImage:[UIImage imageNamed:@"next"] forState:UIControlStateNormal];
        [self.nextButton addTarget:self action:@selector(request:) forControlEvents:UIControlEventTouchUpInside];
    }
    [self.dateButton setTitle:[NSString stringWithFormat:@"%@年 %@期 (%@ - %@)", _pastListModel.year, _pastListModel.no, _pastListModel.beginDateText, _pastListModel.endDateText] forState:UIControlStateNormal];
    [self.dateButton addTarget:self action:@selector(selectDateAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.dateButton setTitleColor:[UIColor colorWithRed:155 / 255.0 green:155 / 255.0 blue:155 / 255.0 alpha:1] forState:UIControlStateNormal];
    self.dateButton.titleLabel.font = [UIFont systemFontOfSize:13];
}

- (void)request:(STLButton *)sender {
    if (sender.tag == 0) {
        NSLog(@"1");
        if (_pastListModel.prevDateCode.intValue != 0) {
            [self.delegate requestWithDateCode:[NSString stringWithFormat:@"%@", _pastListModel.prevDateCode]];
        }
        
    } else {
        NSLog(@"2");
        if (_pastListModel.nextDateCode.intValue != 0) {
            [self.delegate requestWithDateCode:[NSString stringWithFormat:@"%@", _pastListModel.nextDateCode]];
        }
        
    }
}

- (void)selectDateAction:(STLButton *)sender {
    NSLog(@"3");
    [self.delegate creatAlertController];
}
@end
